package com.zhejiangzhengyuan.municipal_sewage.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhejiangzhengyuan.municipal_common.entity.Problems;
import com.zhejiangzhengyuan.municipal_common.entity.vo.ProblemsVo;
import com.zhejiangzhengyuan.municipal_common.response.ResponseVo;
import com.zhejiangzhengyuan.municipal_common.service.ProblemsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 前端控制器
 *
 * @author linju
 * @since 2019-12-12
 */
@RestController
@RequestMapping("/sewage/problems")
@Api(tags = "市政管网模块")
public class ProblemsController {
  @Autowired private ProblemsService problemsService;

  @ApiOperation(value = "新增问题点", response = Problems.class)
  @PostMapping
  public ResponseVo createProblems(@ApiParam("问题点对象") @ModelAttribute Problems problems) {
    return problemsService.saveProblems(problems);
  }

  @ApiOperation(value = "添加问题点图片", response = Problems.class)
  @PostMapping("/file")
  public ResponseVo createProblemsFile(
      Long id,
      @ApiParam(value = "整改前文件", name = "beforeFile")
          MultipartFile beforeFile,
      @ApiParam(value = "整改后文件", name = "file") MultipartFile file) {
    return problemsService.insertProblemsFile(id, beforeFile, file);
  }

  @ApiOperation(value = "修改问题点", response = Problems.class)
  @PostMapping("/update")
  public ResponseVo editProblems(
      @ApiParam("问题点对象") @ModelAttribute Problems problems, String linkList) {
    return problemsService.updateProblems(problems, linkList);
  }

  @ApiOperation(value = "根据Id获取Problems", response = Problems.class)
  @GetMapping("/{id}")
  public ResponseVo getProblemsById(@PathVariable Long id) {
    return  problemsService.getVoById(id);
  }

  @ApiOperation(value = "根据Id删除Problems")
  @DeleteMapping("/{id}")
  public ResponseVo deleteProblemsById(@PathVariable Long id) {
    return problemsService.removeProblems(id);
  }

  @ApiOperation("根据ID 批量删除问题点 格式：'ids':[7,9,10]")
  @DeleteMapping("/removeByIds")
  public ResponseVo removeDeviceByIds(
      @ApiParam(name = "ids:[1,2,3]", required = true) @RequestBody Map<String, List<Long>> map) {
    List<Long> idList = map.get("ids");
    if (Objects.isNull(idList) || idList.isEmpty()) {
      return ResponseVo.CLIENT_PARAM_ERROR;
    }
    return problemsService.removeProblemsByIds(idList);
  }

  @ApiOperation(value = "分页获取问题点", response = ProblemsVo.class)
  @GetMapping("/getByPage")
  public ResponseVo getDeviceVo(
      Integer page,
      Integer limit,
      String area,
      String projectNum,
      String defectItemCode,
      String problemsCode,
      String startTimestamp,
      String endTimestamp) {
    page = page == null || page <= 0 ? 1 : page;
    limit = limit == null || limit <= 0 ? 15 : limit;
    Page<ProblemsVo> voPage = new Page<>(page, limit);
    return ResponseVo.successWithData(
        problemsService.selectVoPage(
            voPage, area, projectNum, defectItemCode, problemsCode, startTimestamp, endTimestamp));
  }
}
